# # from django import forms
# # from django.shortcuts import render, redirect
# # from django.http import JsonResponse
# # from django.core.exceptions import ValidationError
# # from django.views.decorators.csrf import csrf_exempt
# #
# # from accounts import models
# # from accounts.utils.bootstrap import BootStrapModelForm
# # from accounts.utils.encrypt import md5
# # from accounts.utils.pagination import Pagination
# #
# #
# # class AdminModelForm(BootStrapModelForm):
# #     confirm_password = forms.CharField(
# #         label="确认密码",
# #         widget=forms.PasswordInput
# #     )
# #
# #     class Meta:
# #         model = models.Admin
# #         fields = ['username', 'password', 'confirm_password']
# #         widgets = {
# #             "password": forms.PasswordInput
# #         }
# #
# #     def clean_password(self):
# #         pwd=self.cleaned_data.get('password')
# #         return md5(pwd)
# #
# #     def clean_confirm_password(self):
# #         pwd = self.cleaned_data.get('password')
# #         confirm = md5(self.cleaned_data.get("confirm_password"))
# #         if confirm != pwd:
# #             raise ValidationError('密码不一致')
# #         # return的是什么，保存到数据库就是什么
# #         return confirm
# #
# #
# # def admin_list(request):
# #     """ 管理员列表 """
# #     querySet = models.Admin.objects.all().order_by("-id")
# #     page_obj = Pagination(request,querySet)
# #     form = AdminModelForm()
# #     context = {
# #         'querySet': querySet,
# #         'form':form,
# #         "page_string":page_obj.html()
# #     }
# #     return render(request, 'admin_list.html', context)
# #
# #
# # @csrf_exempt
# # def admin_add(request):
# #     """ 添加管理员 """
# #     form = AdminModelForm(data=request.POST)
# #     if form.is_valid():
# #         form.save()
# #         return JsonResponse({"status":True})
# #     return JsonResponse({"status":False,'error':form.errors})
# #
# #
# # def admin_delete(request):
# #     uid = request.GET.get('uid')
# #     models.Admin.objects.filter(id=uid).delete()
# #     return JsonResponse({"status": True})
# #
# #
# # def admin_detail(request):
# #     uid = request.GET.get('uid')
# #     row_dict = models.Admin.objects.filter(id=uid).values('username','password').first()
# #     if not row_dict:
# #         return JsonResponse({"status": False,'error':'数据不存在'})
# #     result = {
# #         'status':True,
# #         "data":row_dict
# #     }
# #     return JsonResponse(result)
# #
# #
# # @csrf_exempt
# # def admin_edit(request):
# #     uid = request.GET.get('uid')
# #     row_obj = models.Admin.objects.filter(id=uid).first()
# #     if not row_obj:
# #         return JsonResponse({'status':False,'tips':"数据不存在,请刷新重试"})
# #     form = AdminModelForm(data=request.POST,instance=row_obj)
# #     if form.is_valid():
# #         form.save()
# #         return JsonResponse({"status":True})
# #     return JsonResponse({"status":False,"error":form.errors})
# #
#
# from django.shortcuts import render, redirect, get_object_or_404
# from django.contrib.auth.decorators import login_required, user_passes_test
# from django.core.paginator import Paginator
# from django.contrib import messages
# from accounts.models import User, CultureProfile
# from tourist_spot.models import TouristSpot
# from accounts.forms import UserAdminForm, CultureProfileForm, TouristSpotReviewForm
#
# def is_admin(user):
#     return user.is_authenticated and (user.user_type == 'admin' or user.is_superuser)
#
# # @login_required
# # @user_passes_test(is_admin)
# def dashboard(request):
#     """管理员仪表盘"""
#     stats = {
#         'user_count': User.objects.count(),
#         'culture_count': CultureProfile.objects.count(),
#         'pending_spots': TouristSpot.objects.filter(status='pending').count(),
#         'latest_spots': TouristSpot.objects.filter(status='pending').order_by('-created_at')[:5]
#     }
#     return render(request, 'admin/dashboard.html', stats)
#
# # @login_required
# # @user_passes_test(is_admin)
# def user_list(request):
#     """用户列表"""
#     users = User.objects.all().order_by('-date_joined')
#     paginator = Paginator(users, 10)
#     page_number = request.GET.get('page')
#     page_obj = paginator.get_page(page_number)
#     return render(request, 'admin/user_list.html', {'page_obj': page_obj})
#
# # @login_required
# # @user_passes_test(is_admin)
# def edit_user(request, user_id):
#     """编辑用户"""
#     user = get_object_or_404(User, pk=user_id)
#     if request.method == 'POST':
#         form = UserAdminForm(request.POST, instance=user)
#         if form.is_valid():
#             form.save()
#             messages.success(request, '用户信息更新成功')
#             return redirect('admin:user_list')
#     else:
#         form = UserAdminForm(instance=user)
#     return render(request, 'admin/edit_user.html', {'form': form})
#
# # @login_required
# # @user_passes_test(is_admin)
# def culture_list(request):
#     """文旅机构列表"""
#     cultures = CultureProfile.objects.all().order_by('-id')
#     paginator = Paginator(cultures, 10)
#     page_number = request.GET.get('page')
#     page_obj = paginator.get_page(page_number)
#     return render(request, 'admin/culture_list.html', {'page_obj': page_obj})
#
# # @login_required
# # @user_passes_test(is_admin)
# def edit_culture(request, culture_id):
#     """编辑文旅机构"""
#     culture = get_object_or_404(CultureProfile, pk=culture_id)
#     if request.method == 'POST':
#         form = CultureProfileForm(request.POST, instance=culture)
#         if form.is_valid():
#             form.save()
#             messages.success(request, '文旅机构信息更新成功')
#             return redirect('admin:culture_list')
#     else:
#         form = CultureProfileForm(instance=culture)
#     return render(request, 'admin/edit_culture.html', {'form': form})
#
# # @login_required
# # @user_passes_test(is_admin)
# def spot_review_list(request):
#     """待审核景点列表"""
#     spots = TouristSpot.objects.filter(status='pending').order_by('-created_at')
#     paginator = Paginator(spots, 10)
#     page_number = request.GET.get('page')
#     page_obj = paginator.get_page(page_number)
#     return render(request, 'admin/spot_review_list.html', {'page_obj': page_obj})
#
# # @login_required
# # @user_passes_test(is_admin)
# def review_spot(request, spot_id):
#     """审核景点"""
#     spot = get_object_or_404(TouristSpot, pk=spot_id)
#     if request.method == 'POST':
#         form = TouristSpotReviewForm(request.POST, instance=spot)
#         if form.is_valid():
#             form.save()
#             messages.success(request, f'景点"{spot.name}"审核状态已更新')
#             return redirect('admin:spot_review_list')
#     else:
#         form = TouristSpotReviewForm(instance=spot)
#     return render(request, 'admin/review_spot.html', {'form': form, 'spot': spot})
